package com.mattgarner.jaddas.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public final class ByteConverter {

	public static final String DEFAULT_STRING_ENCODING = "UTF-8";
	
	
	public static final byte[] convertToBytes(final Object object) throws IOException {

		if(object instanceof String) {
			return convertToBytes((String)object);
		} else if(object instanceof Integer) {
			return convertToBytes((int)(Integer)object);
		} else if(object instanceof Short) {
			return convertToBytes((short)(Short)object);
		} else if(object instanceof Long) {
			return convertToBytes((long)(Long)object);
		} else if(object instanceof Byte[]) {
			return (byte[])object;
		} else {
			return convertToBytes(object.toString());
		}
	}
	
	public static final byte[] convertToBytes(final int integer) throws IOException {
	      ByteArrayOutputStream bos = new ByteArrayOutputStream();
	      DataOutputStream dos = new DataOutputStream(bos); 
    	  dos.writeInt(integer);
    	  dos.flush();
    	  return bos.toByteArray();
	}
	
	public static final byte[] convertToBytes(final short integer) throws IOException {
	      ByteArrayOutputStream bos = new ByteArrayOutputStream();
	      DataOutputStream dos = new DataOutputStream(bos); 
	  	  dos.writeShort(integer);
	  	  dos.flush();
	  	  return bos.toByteArray();
	}
	public static final byte[] convertToBytes(final long integer) throws IOException {
	      ByteArrayOutputStream bos = new ByteArrayOutputStream();
	      DataOutputStream dos = new DataOutputStream(bos);
	      dos.writeLong(integer);
	      dos.flush();
	      return bos.toByteArray();
	}
	
	public static final byte[] convertToBytes(String string) throws IOException  {
		try {
			return string.getBytes(DEFAULT_STRING_ENCODING);
		} catch(Exception e) {
			throw new IOException(e.getMessage());
		}
	}
	
	//
	
	public static final String convertToString(final byte[] bytes) throws IOException {
		return convertToString(bytes, DEFAULT_STRING_ENCODING);
	}
	
	public static final String convertToString(final byte[] bytes, final String encoding) throws IOException {
		try {
			return new String(bytes, encoding);
		} catch(Exception e) {
			throw new IOException(e.getMessage());
		}
	}
	
	public static final short bytesToShort(final byte[] bytes) throws IOException {
	      ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
	      DataInputStream dis = new DataInputStream(bis);
	      short out = dis.readShort();
    	  dis.close();
    	  bis.close();
    	  return out;
	}
	
	public static final int bytesToInt(final byte[] bytes) throws IOException {
	      ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
	      DataInputStream dis = new DataInputStream(bis);
    	  int out = dis.readInt();
    	  dis.close();
    	  bis.close();
    	  return out;	      
	}
	
	public static final long bytesToLong(final byte[] bytes) throws IOException {
	      ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
	      DataInputStream dis = new DataInputStream(bis);
	      long out = dis.readLong();
    	  dis.close();
    	  bis.close();
    	  return out;
	}
	
}
